<?php
namespace app\manage\controller;
use think\Db;

class PayGame extends Base
{
    public function index(){
        if(request()->isAjax() || input('?export')){
            $page = input('page');
            $limit = input('limit');
            $plat = input('plat');
            $date = explodeTime(input('date'));
            $gameId = input('game_id');
            $debug = input('debug');
            $payType = input('pay_type');

            $where = '1';
            $where .= model('manage/Game')->gameClient(input('game_server'), input('game_publish'), input('game_platform'), input('game_client'));
            $where .= " && pay_time between '{$date['start_time']}' AND '{$date['end_time']}'";
            if(!empty($plat)){
                if($plat == 'cps'){
                    $where .= " && channel_id='1000'";
                }else{
                    $where .= " && channel_id>='5000'";
                }
            }
            !empty($gameId) && $where .= " && game_id='{$gameId}'";
            if(!empty($payType)){
                if($payType == 'wxpay'){
                    $where .= " && left(pay_type, 5)='{$payType}'";
                }else{
                    $where .= " && left(pay_type, 6)='{$payType}'";
                }
            }
            if(!empty($gameResult)){
                if($gameResult == 1){
                    $where .= " && game_result='{$gameResult}'";
                }else{
                    $where .= " && game_result='0'";
                }
            }
            if(!empty($debug)){
                if($debug == 1){
                    $where .= " && debug='{$debug}'";
                }else{
                    $where .= " && debug='0'";
                }
            }

            if(input('?export')){
                $start = 0;
                $length = 0;
            }else{
                $start = ($page-1)*$limit;
                $length = $limit;
            }

            $y1 = date('Y', $date['start_time']);
            $y2 = date('Y', $date['end_time']);

            //按天分组
            if($gameId){
                $fieldGame = "game_id, sum(total_price) as total_price, FROM_UNIXTIME(pay_time, '%Y-%m-%d') as date_time";
                $groupGame = 'game_id, date_time';
                $orderGame = 'date_time desc';
            }else{
                $fieldGame = 'game_id, sum(total_price) as total_price';
                $groupGame = 'game_id';
                $orderGame = 'total_price desc';
            }

            if($y1 == $y2){
                $count = Db::name('PayOrderY_'.$y1)->field($fieldGame)->where($where)->group($groupGame)->count();

                $select = Db::name('PayOrderY_'.$y1)
                    ->field($fieldGame)
                    ->where($where)
                    ->group($groupGame)
                    ->order($orderGame)
                    ->limit($start, $length)
                    ->select();

                $sum = Db::name('PayOrderY_'.$y1)
                    ->where($where)
                    ->sum('total_price');
            }else{
                $field = "game_id, total_price, pay_time";
                $subQuery = Db::name('PayOrderY_'.$y1)
                    ->field($field)
                    ->where($where)
                    ->union("select {$field} from sdk_pay_order_y_{$y2} where ({$where})", true)
                    ->buildSql();

                $count = Db::table($subQuery.' y')->field($fieldGame)->group($groupGame)->count();

                $select = Db::table($subQuery.' y')
                    ->field($fieldGame)
                    ->group($groupGame)
                    ->order($orderGame)
                    ->limit($start, $length)
                    ->select();

                $sum = Db::table($subQuery.' y')->sum('total_price');
            }

            $data = [];
            $gameIdName = model('Game')->gameIdName();
            foreach ($select as $k=>$v){
                $data[] = [
                    'daytime' => $gameId ? $v['date_time'] : input('date'),
                    'game_id' => $v['game_id'],
                    'game_name' => isset($gameIdName[$v['game_id']]) ? $gameIdName[$v['game_id']] : '',
                    'total_price' => round($v['total_price']/100, 2)
                ];
            }

            if(input('?export')){
                array_unshift($data, ['支付时间', '游戏ID', '游戏名称', '充值金额']);
                exportExcel($data, input('date').'游戏充值', input('date').'游戏充值');
            }

            $this->layTable(0, '游戏充值', $data, $count, round($sum/100, 2));
        }else{
            $date = date('Y-m-d', time()).' - '.date('Y-m-d', time());
            return view('', ['date'=>$date, 'date2'=>'2018-01 - 2018-01']);
        }
    }
}